package com.edgardleal.tree;

import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DataMiner {

	public static final String URL_PATTERN = "(http://[a-zA-Z]+\\.[(com)(org)(net)]+[^\\? '" + (char)34 + "])";
	public static final String LINK_PATTERN = "[(src)(href)]=['" + (char)34 + "](.*)['" + (char)34 + "]";
	public DataMiner() {

	}

	public String getHTML(String adress) {
		String result = "";
		if (adress.indexOf("http") < 0)
			adress = "http://" + adress;
		URLConnection con = null;
		try {
			String line = "";
			URL url = new URL(adress);
			con = url.openConnection();
			Scanner in = new Scanner(con.getInputStream());
			while ((line = in.nextLine()) != null)
				result += "\n" + line;
			return result;
		} catch (Exception e) {
			return result;
		}
	}

	public ArrayList<String> getAdress(String html) {
		Pattern pattern = Pattern.compile(LINK_PATTERN);
		Matcher matcher = pattern.matcher(html);
		ArrayList<String> results = new ArrayList<String>();
		int index = 0;
		while (matcher.find(index)) {
			index = matcher.end();
			results.add(getURLfromHTMLTag(html.substring(matcher.start(),index)));
		}
		return results;
	}
	
	public String getURLfromHTMLTag(String HTMLTag){
		Matcher m = Pattern.compile(LINK_PATTERN).matcher(HTMLTag);
		return m.group(1);
	}

	public static void main(String[] args) throws Exception{
		DataMiner data = new DataMiner();
		ArrayList<String> urls = data.getAdress(data.getHTML("http://cobaia.net"));
		//String teste = "http://edgardleal.com?a=1".replaceAll(URL_PATTERN, "XXXXX");
		//System.out.println(teste);
		for (int i = 0; i < urls.size(); i++) {
			System.out.println(urls.get(i));
		}
		//System.out.println(new URL("http://www.google.com").getHost());
	}
}
